package com.hangim.pce.service.impl;

import com.hangim.pce.common.search.annotation.ElasticSearch;
import com.hangim.pce.common.search.enums.ESActionType;
import com.hangim.pce.common.search.enums.ESEntityType;
import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import com.hangim.pce.enums.RuleTargetType;
import com.hangim.pce.feign.UserService;
import com.hangim.pce.mapper.BaseDrugMapper;
import com.hangim.pce.model.BaseDrug;
import com.hangim.pce.model.SysDrugRule;
import com.hangim.pce.model.SysDrugUsageRule;
import com.hangim.pce.model.vo.request.DrugVo;
import com.hangim.pce.service.BaseDrugService;
import com.hangim.pce.service.SysDrugRuleService;
import com.hangim.pce.service.SysDrugUsageRuleService;
import com.github.pig.common.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 基本药品表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class BaseDrugServiceImpl extends BaseEntityServiceImpl<BaseDrugMapper, BaseDrug> implements BaseDrugService {

    @Autowired
    private SysDrugUsageRuleService sysDrugUsageRuleService;

    @Autowired
    private SysDrugRuleService sysDrugRuleService;

    @Autowired
    private BaseDrugService baseDrugService;

    @Autowired
    private UserService userService;

    @Override
    @ElasticSearch(entity = ESEntityType.DRUG, action = ESActionType.SYNC)
    public BaseDrug saveDrug(BaseDrug entity) {
        save(entity);
        return entity;
    }

    @Override
    public void synDrug() {

    }

    @Override
    @ElasticSearch(entity = ESEntityType.DRUG, action = ESActionType.REMOVE)
    public BaseDrug deleteDrug(BaseDrug entity) {
        save(entity);
        return entity;
    }

    @Override
    public DrugVo getRules(Long id) {
        DrugVo vo = new DrugVo();
        BaseDrug drug = baseDrugService.selectById(id);
        if (drug != null) {
            vo.setDrugId(String.valueOf(drug.getId()));
            vo.setDrugName(drug.getName());
            vo.setDrugRules(sysDrugRuleService.getDrugRulesByDrugId(id));
            vo.setDrugUsageRules(sysDrugUsageRuleService.getDrugUsageRulesByDrugId(id));
        }
        return vo;
    }


    @Override
    public Boolean saveRules(DrugVo drugVo) {
        if (drugVo != null && drugVo.getDrugId() != null) {
            //先删除所有规则
            SysDrugRule drugRule = new SysDrugRule();
            drugRule.setDrugId(Long.parseLong(drugVo.getDrugId()));
            sysDrugRuleService.delete(drugRule);
            //保存药品规则
            if (drugVo.getDrugRules() != null && drugVo.getDrugRules().size() >= 1) {
                for (SysDrugRule rule : drugVo.getDrugRules()) {
                    rule.setDrugId(Long.parseLong(drugVo.getDrugId()));
                    rule.setDrugName(drugVo.getDrugName());
                    if (rule.getId() != null) {
                        rule.setCreatedAt(new Date());
                        rule.setCreatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    }
                    rule.setUpdatedAt(new Date());
                    rule.setUpdatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    rule.setActive(true);
                    sysDrugRuleService.insert(rule);
                }
            }
            //先删除所有规则
            SysDrugUsageRule sysDrugUsageRule = new SysDrugUsageRule();
            sysDrugUsageRule.setDrugId(Long.parseLong(drugVo.getDrugId()));
            sysDrugUsageRuleService.delete(sysDrugUsageRule);
            //保存药品用法规则
            if (drugVo.getDrugUsageRules() != null && drugVo.getDrugUsageRules().size() >= 1) {
                for (SysDrugUsageRule rule : drugVo.getDrugUsageRules()) {
                    rule.setDrugId(Long.parseLong(drugVo.getDrugId()));
                    rule.setDrugName(drugVo.getDrugName());
                    rule.setType(RuleTargetType.DRUG.getNum());
                    if (rule.getId() != null) {
                        rule.setCreatedAt(new Date());
                        rule.setCreatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    }
                    if (rule.getUsages() != null && rule.getUsages().size() >= 1) {
                        StringBuilder builder = new StringBuilder();
                        for (Integer ints : rule.getUsages()) {
                            builder.append(ints);
                            builder.append(";");
                        }
                        rule.setUsage(builder.substring(0, builder.length() - 1));
                    }
                    rule.setUpdatedAt(new Date());
                    rule.setUpdatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    rule.setActive(true);
                    sysDrugUsageRuleService.insert(rule);
                }
            }
        }
        return true;
    }
}
